﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Alos.Common
{
    public static class StringHelper
    {
        public static bool IsNull(this string str)
        {
            return string.IsNullOrWhiteSpace(str);
        }
        public static bool IsSpeaterBy(this string str, string _char = ",")
        {
            if (string.IsNullOrEmpty(str))
                return false;
            return Regex.IsMatch(str, @"^[1-9][0-9]{0,9}([" + _char + "][0-9]{0,10})*|0$");
        }
        public static bool NotNull(this string str)
        {
            return !string.IsNullOrWhiteSpace(str);
        }
        public static string RemoveComma(this string str, char chr = ',')
        {
            return str.Trim(new char[] { chr });
        }
        public static string[] Split(this string str, string splitStr)
        {
            return str.Split(new string[] { splitStr }, StringSplitOptions.RemoveEmptyEntries);
        }
        public static bool IsFilePath(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return false;
            return Regex.IsMatch(str, @"^(/.*)+[.].+$");
        }
        public static string FormatCode(this string id, int len)
        {
            for (int i = len - id.Length; i > 0; i--)
            {
                id = "0" + id;
            }
            return id;
        }
        public static List<string> GetSrcFile(string strInput)
        {
            List<string> hs = new List<string>();
            if (string.IsNullOrWhiteSpace(strInput)) return hs;
            //获取远程图片url集合
            string _pattern = @"src[ ]*=[ ""']*([^ ""']+[.][^ ""']+]*)[ ""']?";
            MatchCollection found = null;
            RegexOptions TheOptions;
            TheOptions = RegexOptions.Singleline | RegexOptions.IgnoreCase;
            Regex r = new Regex(_pattern, TheOptions);
            found = r.Matches(strInput);

            foreach (Match m in found)
            {
                if (!m.Groups[1].Value.IsFilePath())
                    continue;
                hs.Add(m.Groups[1].Value);
            }
            return hs;
        }
        public static string TimeSpanToString(TimeSpan ts)
        {
            int d = ts.Days;
            int y = (int)Math.Floor((double)(d / 365));
            int M = (int)Math.Floor((double)(d / 30));
            int h = ts.Hours;
            int m = ts.Minutes;
            int s = ts.Seconds;
            string rtnStr = string.Empty;
            rtnStr = y > 0 ? y + "年前" : "";
            rtnStr = string.IsNullOrEmpty(rtnStr) ? M > 0 ? M + "月前" : "" : rtnStr;
            rtnStr = string.IsNullOrEmpty(rtnStr) ? d > 0 ? d + "天前" : "" : rtnStr;
            rtnStr = string.IsNullOrEmpty(rtnStr) ? h > 0 ? h + "小时前" : "" : rtnStr;
            rtnStr = string.IsNullOrEmpty(rtnStr) ? m > 0 ? m + "分钟前" : "" : rtnStr;
            rtnStr = string.IsNullOrEmpty(rtnStr) ? s > 0 ? s + "秒钟前" : "" : rtnStr;
            rtnStr = string.IsNullOrWhiteSpace(rtnStr) ? "刚刚" : rtnStr;
            return rtnStr;
        }

    }
}
